<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8" >
          <el-input
          clearable
          placeholder="请输入内容"
          v-model="query.query"
          @clear="removeSearch">
            <el-button
            slot="append"
            icon="el-icon-search"
            @click="getGoodList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="success" plain @click="toAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表单数据 -->
      <el-table :data="goodList" border stripe style="width: 100%">
         <el-table-column align="center" label="*" type="index">
        </el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="700">
        </el-table-column>
        <el-table-column align="center" label="商品价格(￥)" prop="goods_price">
        </el-table-column>
        <el-table-column align="center" label="商品重量" prop="goods_weight">
        </el-table-column>
        <el-table-column align="center" label="发布时间" >
          <!-- 格式化时间 -->
          <template slot-scope="scope">
          <span>{{scope.row.upd_time | time}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="small"
            type="warning"
            icon="el-icon-edit"
            plain
            ></el-button>
            <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            plain
            @click="deleteGood(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="query.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'GoodList',
  data () {
    return {
      goodList: [], // 商品列表
      query: {
        query: '', // 内容
        pagenum: 1, // 页码
        pagesize: 5 // 数据条数
      },
      total: 0 // 总条数
    }
  },
  created () {
    this.getGoodList()
  },
  methods: {
    // 获取商品列表
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.query
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      // 返回数据
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    // 绑定条数
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getGoodList()
    },
    // 绑定页数
    handleCurrentChange (val) {
      this.query.pagenum = val
      this.getGoodList()
    },
    // 删除
    async deleteGood (goodsId) {
      const confirmRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes !== 'confirm') {
        return this.$message.error('取消删除')
      }
      const { data: res } = await this.$http.delete(`goods/${goodsId}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getGoodList()
    },
    // 清空搜索内容
    removeSearch () {
      this.query.query = ''
      this.getGoodList()
    },
    // 跳转添加
    toAddGoods () {
      this.$router.push('/goods/addgood')
    }
  }
}
</script>

<style lang="less" scoped></style>
